﻿namespace Magic.Redis;


/// <summary>
/// 提供Redis客户端操作类（基于CsRedisCore实现）
/// </summary>
public sealed partial class CsRedisCoreClient : IRedisClient
{
	/// <summary>
	/// 删除传入的 key，命令：del {key}
	/// </summary>
	/// <param name="key">要移除的key</param>
	/// <returns>返回<see cref="bool"/>表示移除成功或失败，key不存在时将返回<code>false</code>。</returns>
	public bool KeyDelete(string key)
	{
		try
		{
			if (KeyExists(key))
			{
				RedisHelper.Del(key);
				return true;
			}
			return false;
		}
		catch (Exception ex)
		{
			Faild($"An exception occurs on KeyDelete.key={key}", ex);
			return default;
		}
	}

	/// <summary>
	/// （异步的）删除传入的 key，命令：del {key}
	/// </summary>
	/// <param name="key">要移除的key</param>
	/// <returns>返回<see cref="bool"/>表示移除成功或失败。</returns>
	public async Task<bool> KeyDeleteAsync(string key)
	{
		try
		{
			bool exists = await KeyExistsAsync(key);
			if (exists)
			{
				await RedisHelper.DelAsync(key);
				return true;
			}
			return false;
		}
		catch (Exception ex)
		{
			Faild($"An exception occurs on KeyDeleteAsync.key={key}", ex);
			return default;
		}
	}

	/// <summary>
	/// 返回指定的key是否存在，命令：exists {key}
	/// </summary>
	/// <param name="key"></param>
	/// <returns>返回<see cref="bool"/>表示key是否存在。</returns>
	public bool KeyExists(string key)
	{
		try
		{
			return RedisHelper.Exists(key);
		}
		catch (Exception ex)
		{
			Faild($"An exception occurs on KeyExists.key={key}", ex);
			return false;
		}
	}

	/// <summary>
	/// （异步的）返回指定的key是否存在，命令：exists {key}
	/// </summary>
	/// <param name="key"></param>
	/// <returns>返回<see cref="bool"/>表示key是否存在。</returns>
	public async Task<bool> KeyExistsAsync(string key)
	{
		try
		{
			return await RedisHelper.ExistsAsync(key);
		}
		catch (Exception ex)
		{
			Faild($"An exception occurs on KeyExistsAsync.key={key}", ex);
			return default;
		}
	}

	/// <summary>
	/// 为指定的 key 设置过期时间点
	/// </summary>
	/// <param name="key">要设定的 key</param>
	/// <param name="expiry">可选参数，过期时间<see cref="DateTime"/>，在该时间之后，key 会过期，不传递将会设置为即刻过期。</param>
	/// <returns>返回<see cref="bool"/>表示是否设定成功，key不存在时将返回<code>false</code>。</returns>
	public bool KeyExpireAt(string key, DateTime? expiry = null)
	{
		if (!KeyExists(key))
		{
			return false;
		}
		if (!expiry.HasValue)
		{
			expiry = DateTime.Now;
		}
		try
		{
			return RedisHelper.ExpireAt(key, expiry.Value);
		}
		catch (Exception ex)
		{
			Faild($"An exception occurs on KeyExpireAt.key={key}", ex);
			return default;
		}
	}

	/// <summary>
	/// （异步的）为指定的 key 设置过期时间点
	/// </summary>
	/// <param name="key">要设定的 key</param>
	/// <param name="expiry">可选参数，过期时间<see cref="DateTime"/>，在该时间之后，key 会过期，不传递将会设置为即刻过期。</param>
	/// <returns>返回<see cref="bool"/>表示是否设定成功，key不存在时将返回<code>false</code>。</returns>
	public async Task<bool> KeyExpireAtAsync(string key, DateTime? expiry = null)
	{
		bool exists = await KeyExistsAsync(key);
		if (!exists)
		{
			return false;
		}
		if (!expiry.HasValue)
		{
			expiry = DateTime.Now;
		}
		try
		{
			return await RedisHelper.ExpireAtAsync(key, expiry.Value);
		}
		catch (Exception ex)
		{
			Faild($"An exception occurs on KeyExpireAtAsync.key={key}", ex);
			return default;
		}
	}

}
